package com.lzy.cloud.payment.component.pay;

import com.lzy.cloud.payment.constants.BusinessStatus;
import com.lzy.cloud.payment.constants.CommonConstants;
import com.lzy.cloud.payment.dtos.pay.BasePayDTO;
import com.lzy.cloud.payment.dtos.payment.PayInfoDto;
import com.lzy.cloud.payment.dtos.payment.PayWayDto;
import com.lzy.cloud.payment.remote.AccountApi;
import com.lzy.cloud.payment.remote.BaseApi;
import com.lzy.cloud.payment.remote.OrderApi;
import com.lzy.cloud.payment.remote.dtos.AccountWalletDto;
import com.lzy.cloud.payment.remote.dtos.AccountWalletInfoDto;
import com.lzy.cloud.payment.remote.dtos.OrderExpenseItemDto;
import com.lzy.cloud.payment.remote.request.AccountWalletInfoRequest;
import com.lzy.cloud.payment.remote.request.BaseCouponRequest;
import com.lzy.cloud.payment.remote.request.OrderCouponLogRequest;
import com.lzy.cloud.payment.remote.request.OrderExpenseItemRequest;
import com.lzy.cloud.payment.remote.response.OrderCouponLogResponse;
import com.lzy.cloud.payment.service.PayInfoService;
import com.lzy.cloud.payment.service.PayWayService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

@Component("payCommons")
public abstract class PayCommons<V extends BasePayDTO> {

    private static final Logger logger = LoggerFactory.getLogger(PayCommons.class);

    @Autowired
    private PayInfoService payInfoService;

    @Autowired
    private PayWayService payWayService;

    @Autowired
    private PayTradeCommons payTradeCommons;

    @Autowired
    private OrderApi orderApi;

    @Autowired
    private AccountApi accountApi;

    @Autowired
    private BaseApi baseApi;

    public PayCommons() {

    }

    /**
     * 异步
     *
     * @param v
     * @return
     */
    public abstract String payAsyNotify(V v);

    /**
     * 支付宝-微信
     *
     * @param bpd 异步业务处理
     * @return
     */
    public boolean NotifyProcess(BasePayDTO bpd) {
        //验证支付宝通道是否启用
        PayWayDto payWayDto = payWayService.queryPayWayInfo(CommonConstants.PayWay.getName(bpd.getWay()));
        Ensure.that(payWayDto.getEnabled().equals(CommonConstants.PayWayEnabledStatus.NOUSE.getIndex())).isFalse("09000005");   //支付网管未启用

        PayInfoDto payInfoDO = payInfoService.queryPayInfoByOrderId(bpd.getOut_trade_no());
        Ensure.that(payInfoDO == null).isFalse("09000025");  //支付宝，微信异步返回商户订单号不在数据库中
        //当未支付
        if (payInfoDO.getStatus().equals(CommonConstants.OrderPayStatus.NOPAY.getIndex())) {
            bpd.setUserId(payInfoDO.getMemberId());
            bpd.setType(payInfoDO.getType());

            //更新其他API接口(订单、充值、成为服务商订单)
            if (payTradeCommons.updateResult(bpd)) {
                logger.info("-----其他API接口更新成功-----");
                //交易记录
                payTradeCommons.payInfo(payInfoDO, bpd);

                //订单支付（冻结余额解冻，冻结卡券解冻）
                if(payInfoDO.getType().equals(BusinessStatus.PayInfoType.pay)) {
                    //将订单中的冻结金额扣除
                    payTradeCommons.thawPayment(bpd,payInfoDO);
                }

                //龙币充值
                if(payInfoDO.getType().equals(BusinessStatus.PayInfoType.lbrecharge)) {
                    //根据order_id获取订单详情
                     orderApi.findOrderByOrderId(payInfoDO.getSerialNumber());


                }

                //龙珠充值
                return true;
            }
        }
        return false;
    }

}
